"""
name: 教学案例2 使用ui文件布局，事件处理
version: 1.0
"""
import sys
from typing import Any
from PyQt5.QtCore import QEvent
from PyQt5.QtGui import QCloseEvent
from PyQt5.QtWidgets import QApplication, QWidget
from ui_example import Ui_Form

class MyDialog(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_Form() 
        self.ui.setupUi(self)
        self.ui.textEdit.setPlainText('Hello World!')  # 设置textEdit的文本内容
        self.ui.textEdit.setReadOnly(True)  # 设置textEdit的文本为只读，不可编辑

    def event(self, a0: QEvent) -> Any:
        # 打印事件类型
        print('event: ', a0.type())
        # 如果事件类型是鼠标按钮按下事件
        if a0.type() == QEvent.MouseButtonPress:
            # 打印鼠标按钮按下事件
            print('event:  鼠标按钮按下事件')
        # 返回父类的event方法
        return super().event(a0)
    def closeEvent(self, a0: QCloseEvent) -> None:
        # 打印关闭事件类型
        print('closeEvent: ', a0.type())
        # 调用父类的closeEvent方法
        super().closeEvent(a0)

app = QApplication(sys.argv)
# 创建自定义界面MyDialog的实例（对象）
dialog = MyDialog()
dialog.show()
sys.exit(app.exec_())
